import { 
  CONFIRM_USER_REQUEST, 
  CONFIRM_USER_FAILURE, 
  CONFIRM_USER_SUCCESS,
  RESET_USER_REQUEST, 
  RESET_USER_FAILURE, 
  RESET_USER_SUCCESS,
  GET_CODE_REQUEST, 
  GET_CODE_FAILURE, 
  GET_CODE_SUCCESS, 
  GET_CODE_OVER_TIME,
  CLEAR_ERROR
} from '../constants';
import CONFIG from '../../config';
import { checkHttpStatus } from '../Helper';
import { push } from 'react-router-redux';

export function getCodeRequest() {
  return { 
    type: GET_CODE_REQUEST
  }
}

export function getCodeFailure(error) {
  return {
    type: GET_CODE_FAILURE,
    payload: error
  }
}

export function getCodeSuccess() {
  return {
    type: GET_CODE_SUCCESS
  }
}

export function getCodeOverTime() {
  return {
    type: GET_CODE_OVER_TIME
  }
}

export function sendCode(phone) {
  return (dispatch) => {
    dispatch(getCodeRequest());
    return fetch(CONFIG.apiHost + '/api/verification', {
      method: 'post',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        phone: phone
      })
    })
    .then(checkHttpStatus)
    .then(res => res.json())
    .then(res => {
      if (res.status) {
        dispatch(getCodeSuccess());
        setTimeout(function(){dispatch(getCodeOverTime())}, 90000);
        // 90秒后可以再次获取验证码
      }
    })
    .catch(e => {
      Promise.resolve(e.res.json()).then(v => {
        dispatch(resetUserFailure(v.msg));
        setTimeout(function(){dispatch(clearError())}, 4000);
      })
    })
  }
}

export function comfirmUserRequest() {
  return {
    type: CONFIRM_USER_REQUEST
  }
}

export function comfirmUserFailure(error) {
  return {
    type: CONFIRM_USER_FAILURE,
    payload: error
  }
}

export function comfirmUserSuccess() {
  return {
    type: CONFIRM_USER_SUCCESS
  }
}

export function comfirm(phone, verification_code) {
  return (dispatch) => {
    dispatch(comfirmUserRequest());
    return fetch(CONFIG.apiHost + '/api/reset/comfirm', {
      method: 'post',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        phone: phone,
        verification_code: verification_code
      })
    })
    .then(checkHttpStatus)
    .then(res => res.json())
    .then(res => {
      if (res.status) {
        dispatch(comfirmUserSuccess());
      }
    })
    .catch(e => {
      Promise.resolve(e.res.json()).then(v => {
        dispatch(comfirmUserFailure(v.msg));
        setTimeout(function(){dispatch(clearError())}, 4000);
      })
    })
  }
}

export function resetUserRequest() {
  return {
    type: RESET_USER_REQUEST
  }
}

export function resetUserFailure(error) {
  return {
    type: RESET_USER_FAILURE,
    payload: error
  }
}

export function resetUserSuccess() {
  return {
    type: RESET_USER_SUCCESS
  }
}

export function reset(phone, verification_code, password, password_confirmation) {
  return (dispatch) => {
    dispatch(resetUserRequest());
    return fetch(CONFIG.apiHost + '/api/reset', {
      method: 'post',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        phone: phone,
        verification_code: verification_code,
        password: password,
        password_confirmation: password_confirmation
      })
    })
    .then(checkHttpStatus)
    .then(res => res.json())
    .then(res => {
      if (res.status) {
        dispatch(resetUserSuccess());
        setTimeout(function(){dispatch(pushTo())}, 4000);
      }
    })
    .catch(e => {
      Promise.resolve(e.res.json()).then(v => {
        dispatch(resetUserFailure(v.msg));
        setTimeout(function(){dispatch(clearError())}, 4000);
      })
    })
  }
}

export function pushTo() {
  return (dispatch) => {
    dispatch(clear());
    dispatch(push('/login'));
  }
}

export function clearError() {
  return {
    type: 'CLEAR_ERROR'
  }
}

export function clear() {
  return {
    type: 'CLEAR'
  }
}